<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">

      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :xl="3" :lg="7" :md="8" :sm="24">
          <a-form-model-item label="科目"  prop="subjectId" style='width: 180px;float: left'>
            <a-select v-model="queryParam.subjectId" placeholder="请选择科目"
                      :getPopupContainer="(target) => target.parentNode" @change="handleSubjectChange"  >
              <a-select-option v-for="item in subjectList" :key="item.value">{{ item.label }}</a-select-option>
            </a-select>

          </a-form-model-item>

          </a-col>
          <a-col :xl="3" :lg="7" :md="8" :sm="24">
          <a-form-model-item label="教师"  prop="teacherId"  style='width: 200px;float: left'>
            <j-multi-select-tag v-model="queryParam.teacherId" :options="teacherList" placeholder="请选择教师"   >
            </j-multi-select-tag>
          </a-form-model-item>
          </a-col>

<!--          <a-col :xl="3" :lg="7" :md="8" :sm="24">-->
<!--            <a-form-model-item label="年级" prop="queryParam.gradeId">-->
<!--              &lt;!&ndash;              v-model="queryParam.gradeId"  对应java的Controller的接收参数vo对象的属性gradeId的值 &ndash;&gt;-->
<!--              <j-dict-select-tag @change='changeGradeId' v-model="queryParam.gradeId" placeholder="请选择年级" dictCode="cou_grade,grade_name,id,tenant_id  is not null order by  grade_order"/>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :xl="4" :lg="7" :md="8" :sm="24">-->
<!--            &lt;!&ndash;                <a-input placeholder="请输入科目" v-model="queryParam.subjectId"></a-input>&ndash;&gt;-->
<!--            <a-form-model-item label="班级" prop="queryParam.classId">-->
<!--              &lt;!&ndash;                <j-select-multiple />   此处使用多选组件时，联动会失效，暂未解决联动失效问题&ndash;&gt;-->
<!--              &lt;!&ndash;                <j-dict-select-tag />   单选正常联动，但无法多选查询&ndash;&gt;-->

<!--              <j-multi-select-tag v-model="queryParam.classId" placeholder="请选择班级" :dictCode="queryParamSelect.classIdSelect" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :xl="3" :lg="7" :md="8" :sm="24">-->
<!--            <a-form-model-item label="教学楼" prop="queryParam.buildingId">-->
<!--              &lt;!&ndash;              v-model="queryParam.gradeId"  对应java的Controller的接收参数vo对象的属性gradeId的值 &ndash;&gt;-->
<!--              <j-dict-select-tag @change='changeBuildingId' v-model="queryParam.buildingId" placeholder="请选择教学楼" dictCode="fac_building,building_name,id"/>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :xl="3" :lg="7" :md="8" :sm="24">-->
<!--            &lt;!&ndash;                <a-input placeholder="请输入科目" v-model="queryParam.subjectId"></a-input>&ndash;&gt;-->
<!--            <a-form-model-item label="教室" prop="queryParam.classroomId">-->
<!--              <j-multi-select-tag placeholder="请选择教室" v-model="queryParam.classroomId" :dictCode="queryParamSelect.classroomIdSelect" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
        <template v-if="toggleSearchStatus">

        </template>


      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
<!--      <a-button type="primary" icon="download" @click="handleExportXls('rel_teacher_subject')">导出</a-button>-->
<!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"-->
<!--                @change="handleImportExcel">-->
<!--        <a-button type="primary" icon="import">导入</a-button>-->
<!--      </a-upload>-->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
                     @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <template>
        <span style="float:right;">
          <a @click="searchQuery()"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <a-button v-print="'#datatable'" ghost type="primary">打印</a-button>
        </span>
      </template>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="relData"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <rel-teacher-subject-modal ref="modalForm" @ok="modalFormOk"></rel-teacher-subject-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import RelTeacherSubjectModal from './modules/RelTeacherSubjectModal'
import { getSubjectList, queryTeacherListBySubject } from '@api/commonAPI'
import { getAction } from '@api/manage'
export default {
  name: 'RelTeacherSubjectList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    RelTeacherSubjectModal
  },
  data() {
    return {
      description: 'rel_teacher_subject管理页面',
      disableMixinCreated : true, //默认false加载数据,true不加载

      //查询条件联动部分定义
      queryParamSelect: {
        classIdSelect: "cou_class,class_name,id,class_iz_open  =1 and class_iz_delete  = 0 order by  class_order",
        classroomIdSelect: "fac_classroom,classroom_name,id",
    },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        // {
        //   title: '教师编码',
        //   align: 'center',
        //   dataIndex: 'teacherId'
        // },
        {
          title: '任课教师',
          align: 'center',
          dataIndex: 'teacherName'
        },
        {
          title: '所任科目',
          align: 'center',
          dataIndex: 'subjectName'
        },
        // {
        //   title: '科目编码',
        //   align: 'center',
        //   dataIndex: 'subjectId'
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/rel_teacher_subject/relTeacherSubject/list',
        delete: '/rel_teacher_subject/relTeacherSubject/delete',
        deleteBatch: '/rel_teacher_subject/relTeacherSubject/deleteBatch',
        exportXlsUrl: '/rel_teacher_subject/relTeacherSubject/exportXls',
        importExcelUrl: 'rel_teacher_subject/relTeacherSubject/importExcel',
        voList: '/rel_teacher_subject/relTeacherSubject/volist'
      },
      queryParam: {
        subjectId: null,
        teacherId: null,
        tenantId: null,
      },
      dictOptions: {},
      superFieldList: [],
      relData: [],

      teacherList: [],
      subjectList: []
    }
  },
  created() {
    this.initList();
    this.getTeacherLists();
    this.getSuperFieldList()
    this.initTale()

  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'teacherId', text: '教师编码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'subjectId', text: '科目编码', dictCode: '' })
      this.superFieldList = fieldList
    },
    //更改年级-->选择考试，选择班级
    changeGradeId(){
      if( this.queryParam.gradeId   ){ //判断不为空
        this.queryParamSelect.classIdSelect = "cou_class,class_name,id,class_iz_open  =1 and class_iz_delete  = 0 and grade_code=\'"+ this.queryParam.gradeId + "\' order by  class_order" ;
      }
    },
    //更改教学楼-->选择教室
    changeBuildingId(){
      if( this.queryParam.buildingId   ){ //判断不为空
        this.queryParamSelect.classroomIdSelect = "fac_classroom,classroom_name,id ,building_id=\'"+ this.queryParam.buildingId + "\'" ;
      }
    },
    initTale() {
      let params = {
        subjectId : this.queryParam.subjectId,
        teacherId : this.queryParam.teacherId
      }
      getAction(this.url.voList,params).then(res => {
        if (res && res.code === 200) {
          this.relData = res.result.records
        } else {
          this.$message.error(res.message)
        }
      })
    },
    searchQuery(){
      this.initTale();
    },
    searchReset(){
      this.queryParam = {}
      this.initTale();
      this.getTeacherLists();
    },
    async initList() {
      let subjectList = await getSubjectList()
      if (subjectList.code === 200) {
        this.subjectList = []
        subjectList.result.forEach(item => {
          this.subjectList.push({
            label: item.subjectName,
            value: item.subjectId
          })
        })
      }
    },
    handleSubjectChange() {
      this.getTeacherLists();
    },
    async getTeacherLists(){
      let tenantId = ''
      if (localStorage.length > 0) {
        let localData = JSON.parse(localStorage.getItem('pro__Login_Userinfo'))
        tenantId = localData.value.tenantId
        this.queryParam.tenantId = tenantId
      }
      let params = {
        tenantId: tenantId,
        subjectId: this.queryParam.subjectId
      }
      let res = await queryTeacherListBySubject(params)
      if (res.success) {
        this.teacherList = []
        res.result.forEach(item => {
          this.teacherList.push({
            value: item.teacherId,
            label: item.teacherName
          })
        })
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>